package com.kaka.bus.engine.borrow.phone;

import android.content.Intent;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.kaka.bus.R;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangqi on 2016/11/4.
 */

public class ForGetServiceActivity extends AppCompatActivity {

    @BindView(R.id.forgetservice)
    PhotoView mForgetservice;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.imageView)
    SubsamplingScaleImageView mScaleImageView;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetservice);
        ButterKnife.bind(this);

        mForgetservice.enable();

        mToolbarBack.setOnClickListener(v -> {
            finish();
        });
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }

        String yunying = intent.getStringExtra("yunying");
        int srcRes = -1;
        if ("移动".equals(yunying)) {
            srcRes = R.mipmap.yidong;
        } else if ("联通".equals(yunying)) {
            srcRes = R.mipmap.liantong;
        } else if ("电信".equals(yunying)) {
            srcRes = R.mipmap.dianxin;
        }else if("合同".equals(yunying)){
            mScaleImageView.setVisibility(View.VISIBLE);
            mForgetservice.setVisibility(View.GONE);
            String url = intent.getStringExtra("url");
            mToolbarName.setText("手机回收合同");
            mScaleImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);

            mScaleImageView.setMinScale(1.3F);//最小显示比例

            mScaleImageView.setMaxScale(3.0F);//最大显示比例（太大了图片显示会失真，因为一般微博长图的宽度不会太宽）

            Glide.with(this)
                    .load(url).downloadOnly(new SimpleTarget<File>() {
                @Override
                public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                    // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                    mScaleImageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(1.0F, new PointF(0, 0), 0));
                }});
            return;
        }

        if (srcRes != -1) {
            mForgetservice.setImageResource(srcRes);
            mToolbarName.setText("中国" + yunying);
        } else {
            mToolbarName.setText("未知错误");
        }
    }
}
